package com.ai.wxy.spring.web.conf;

import com.ai.wxy.spring.service.DefaultServiceCaller;
import com.ai.wxy.spring.service.api.ServiceCaller;
import com.ai.wxy.spring.web.FileUploadMultipartResolver;
import com.ai.wxy.spring.web.argument.RequestParamArgumentResolver;
import com.ai.wxy.spring.web.util.Constans;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.MultipartConfigElement;
import javax.servlet.Servlet;

/**
 * 文件上传相关配置
 *
 * @author 石头
 * @Date 2019/11/15
 * @Version 1.0
 **/
@Configuration
@ConditionalOnClass(value = {Servlet.class,MultipartConfigElement.class})
@EnableConfigurationProperties(value = {FileUploadProperties.class})
public class FileUploadAutoConfiguration {
    @Bean
    public MultipartConfigElement multipartConfigElement(FileUploadProperties properties){
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(DataSize.ofBytes(Constans.parseSize(properties.getMaxFileSize()) * 10));
        factory.setMaxRequestSize(DataSize.ofBytes(Constans.parseSize(properties.getMaxRequestSize()) * 10));

        return factory.createMultipartConfig();
    }

    @Bean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
    @ConditionalOnMissingBean(MultipartResolver.class)
    public FileUploadMultipartResolver multipartResolver(FileUploadProperties properties){
        FileUploadMultipartResolver multipartResolver = new FileUploadMultipartResolver();
        multipartResolver.setWhiteConditionStr(properties.getWhiteConditionStr());
        multipartResolver.setZresolveLazily(properties.getResolveLazily());
        multipartResolver.setMaxFileSize(Constans.parseSize(properties.getMaxFileSize()));
        multipartResolver.setMaxRequestSize(Constans.parseSize(properties.getMaxRequestSize()));

        return multipartResolver;
    }

    @Bean
    @ConditionalOnMissingBean(ServiceCaller.class)
    public ServiceCaller defaultServiceCaller(){
        return new DefaultServiceCaller();
    }

    @Bean
    public RequestParamArgumentResolver RequestParamArgumentResolver(ServiceCaller serviceCaller){
        return new RequestParamArgumentResolver(serviceCaller);
    }

    @Bean
    @ConditionalOnMissingBean(WebMethodArgumentResolverConf.class)
    public WebMethodArgumentResolverConf webMethodArgumentResolverConf(RequestParamArgumentResolver paramArgumentResolver){
        return new WebMethodArgumentResolverConf(paramArgumentResolver);
    }
}
